<template>
  <div class="app-container calendar-list-container">
    <!-- <div class="filter-container">
        <el-date-picker
        v-model="datecon"
        type="datetimerange"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :default-time="['00:00:00']">
        </el-date-picker>
      <el-button v-if="this.btn_query" v-waves style="margin-left: 10px;" type="primary" icon="el-icon-search" @click="handleFilter">{{ $t('table.search') }}</el-button>
      <el-button v-if="this.btn_add" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="handleCreate">{{ $t('table.add') }}</el-button>
    </div> -->

    <el-table
      v-loading="listLoading"
      :key="tableKey"
      :data="list"
      element-loading-text="给我一点时间"
      border
      fit
      highlight-current-row
      style="width: 100%">
      <el-table-column width="120" align="center" label="事件ID">
        <template slot-scope="scope">
          <span>{{ scope.row.eventId }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column width="120" label="关联Token">
        <template slot-scope="scope">
          <span>{{ scope.row.assignmentToken }}</span>
        </template>
      </el-table-column>-->
      <!--<el-table-column width="100" align="center" label="关联类型">
        <template slot-scope="scope">
          <span>{{ scope.row.assignmentType }}</span>
        </template>
      </el-table-column>-->
      <el-table-column width="160" align="center" label="告警消息">
        <template slot-scope="scope">
          <span>{{ scope.row.alertMessage }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="告警位置">
        <template slot-scope="scope">
          <span>{{ scope.row.metadata.location }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120" align="center" label="设施名称">
        <template slot-scope="scope">
          <span>{{ scope.row.metadata.assetName }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="场所名称">
        <template slot-scope="scope">
          <span>{{ scope.row.metadata.siteName }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="传感器名称">
        <template slot-scope="scope">
          <span>{{ scope.row.metadata.categoryname }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="事件日期">
        <template slot-scope="scope">
          <span>{{ dformat("yyyy-MM-dd hh:mm:ss",scope.row.eventDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="接收日期">
        <template slot-scope="scope">
          <span>{{ dformat("yyyy-MM-dd hh:mm:ss",scope.row.receivedDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column width="160" align="center" label="告警类型">
        <template slot-scope="scope">
          <span>{{ scope.row.alertType }}</span>
        </template>
      </el-table-column>

      <el-table-column width="160" align="center" label="告警等级">
        <template slot-scope="scope">
          <span>{{ scope.row.alertLevel }}</span>
        </template>
      </el-table-column>
      <!--<el-table-column width="160" align="center" label="告警来源">
        <template slot-scope="scope">
          <span>{{ scope.row.alertSource }}</span>
        </template>
      </el-table-column>-->

      <el-table-column :label="$t('table.actions')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <!--<el-button v-if="btn_edit" type="primary" size="mini" @click="handleUpdate(scope.row)">{{ $t('table.edit') }}</el-button>
          <el-button v-if="btn_delete" size="mini" type="danger" @click="handleDelete(scope.row)">{{ $t('table.delete') }}</el-button>-->
          <!--<el-button v-if="btn_delete" size="small" type="danger" @click="handleDispelalert(scope.row.eventId)">消除告警</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        :current-page.sync="listQuery.page"
        :page-sizes="[10,20,30, 50]"
        :page-size="listQuery.limit"
        :total="total"
        background
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"/>
    </div>

    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="用户名称" prop="username">
          <el-input v-model="temp.username"/>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="temp.phone"/>
        </el-form-item>
        <el-form-item label="身份证" prop="identification">
          <el-input v-model="temp.identification"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">{{ $t('table.confirm') }}</el-button>
        <el-button v-else type="primary" @click="updateData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>

    <!--消除告警内容填写-->
    <el-dialog title="消除告警" :visible.sync="dispelalertFormVisible">
      <el-form ref="dispeldataForm" :rules="rules" :model="dispelTmp" label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">
        <el-form-item label="现场照片" prop="spotimage">
          <el-input v-model="dispelTmp.spotimage" :disabled="true"/>
          <el-upload
            ref="uploadTool"
            class="upload-demo"
            action=""
            :limit="1"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :file-list="fileList"
            :http-request="getUploadAddress"
            list-type="picture">
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="理由" prop="reason">
          <el-input v-model="dispelTmp.reason" type="textarea"/>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dispelalertFormVisible = false">{{ $t('table.cancel') }}</el-button>
        <el-button type="primary" @click="dispelalertData">{{ $t('table.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getAlertList, dispelalert, uploadPic } from '@/api/fire/proprietor/monitor'
import { dateFormat } from '@/utils/dateFormatter'
import { getToken } from '@/utils/auth'
import waves from '@/directive/waves' // 水波纹指令

export default {
  name: 'monitor',
  directives: {
    waves
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      datecon:'',
      tableKey: 0,
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10,
        startdate:'',
        enddate:'',
        range: this.$route.name,
        role: this.$store.getters.roles[0]
      },
      showReviewer: false,
      temp: {
        eventId : '',
        assignmentToken : '',
        assignmentType : '',
        assetId : '',
        siteToken : '',
        eventDate : '',
        receivedDate : '',
        alertType : '',
        alertMessage : '',
        alertLevel : '',
        alertSource : ''
      },
      dispelTmp:{
        alertid:'',
        spotimage:'',
        reason:''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑用户',
        create: '创建用户'
      },
      dialogPvVisible: false,
      pvData: [],
      rules: {
         username: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
         phone: [{ required: true, message: '请输入电话', trigger: 'blur' }],
         identification: [{ required: true, message: '请输入身份证', trigger: 'blur' }],
      },
      dialogFormRolesVisible: false,
      btn_query: false,
      btn_add: false,
      btn_edit: false,
      btn_delete: false,
      dispelalertFormVisible:false,
      fileList:[],
      file: null
    }
  },
  created() {
    this.getList();
    this.btn_query = this.checkPermission(['proprietor:user:list']);
    this.btn_add = this.checkPermission(['proprietor:user:add']);
    this.btn_edit = this.checkPermission(['proprietor:user:update']);
    this.btn_delete = this.checkPermission(['proprietor:user:delete']);
  },
  methods: {
    getdate(){
      if(this.datecon != ''){
        this.listQuery.startdate=this.datecon.get(0);
        this.listQuery.startdate=this.datecon.get(1);
      }else{
        this.listQuery.startdate = ''
        this.listQuery.startdate = ''
      }
    },
    getList() {
      this.listLoading = true
      getAlertList(this.listQuery).then(response => {
        if(response.data.status == 0){
          this.list = response.data.context.records
          this.total = response.data.context.total
          console.log("list",this.list)
        }
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      //this.getdate()
      this.getList()
    },
    handleSizeChange(val) {
      this.listQuery.limit = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listQuery.page = val
      this.getList()
    },
    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },
    resetTemp() {
      this.temp = {
        commanyname: '',
        companyaddress: '',
        contactname: '',
        commanyphone: '',
        web: '',
        owner: '',
        phone: '',
        email: '',
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addProUser(this.temp).then(response => {
            if (response.data.status === '0') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    updateData() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          const tempData = Object.assign({}, this.temp)
          updateProUser(tempData).then(response => {
            if (response.data.status === '0') {
              this.dialogFormVisible = false
              this.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleDelete(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
        dispelalert(row.userid).then(response => {
            if (response.data.status === '0') {
              this.getList()
              this.$notify({
                title: '成功',
                message: response.data.msg,
                type: 'success',
                duration: 2000
              })
            } else {
              this.$notify.error({
                title: '失败',
                message: response.data.msg,
                duration: 2000
              })
            }
          })
        })

    },
    dformat(fmt, date) {
      return dateFormat(fmt, date)
    },
    resetDispelTmp() {
      this.dispelTmp = {
        alertid:'',
        spotimage:'',
        reason:''
      }
    },
    handleDispelalert(alertid){
      this.resetDispelTmp()
      this.dispelTmp.alertid = alertid
      this.dispelalertFormVisible = true
      this.$refs.uploadTool.clearFiles()
      this.$nextTick(() => {
        this.$refs['dispeldataForm'].clearValidate()
      })
    },
    dispelalertData(){
      this.$confirm("此操作将永久消除告警, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        dispelalert(this.dispelTmp).then(response => {
          if (response.data.status === '0') {
            this.getList()
            this.dispelalertFormVisible = false
            this.$notify({
              title: '成功',
              message: response.data.msg,
              type: 'success',
              duration: 2000
            })
          } else {
            this.$notify.error({
              title: '失败',
              message: response.data.msg,
              duration: 2000
            })
          }
        })
      })
    },
    getUploadAddress(param){
      const formData = new FormData()
      formData.append('file',param.file, param.file.name)
      uploadPic(formData).then(response=>{
        this.dispelTmp.spotimage = response.data.context
      })
    },
    handlePreview(file){
      console.log(file)
    },
    handleRemove(file, fileList){
      this.dispelTmp.spotimage = ''
    }
  }
}
</script>
